home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / lrand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  1.1 KB  |  57 lines

  1. #include "lib.h"
  2.  
  3. /*  lrand(3)
  4.  *
  5.  *  Author: Terrence W. Holm          Nov. 1988
  6.  *
  7.  *
  8.  *  A prime modulus multiplicative linear congruential
  9.  *  generator (PMMLCG), or "Lehmer generator".
  10.  *  Implementation directly derived from the article:
  11.  *
  12.  *    S. K. Park and K. W. Miller
  13.  *    Random Number Generators: Good Ones are Hard to Find
  14.  *    CACM vol 31, #10. Oct. 1988. pp 1192-1201.
  15.  *
  16.  *
  17.  *  Using the following multiplier and modulus, we obtain a
  18.  *  generator which:
  19.  *
  20.  *    1)  Has a full period: 1 to 2^31 - 2.
  21.  *    2)  Is testably "random" (see the article).
  22.  *    3)  Has a known implementation by E. L. Schrage.
  23.  */
  24.  
  25.  
  26. #define  A      16807L    /*  A "good" multiplier      */
  27. #define  M   2147483647L    /*  Modulus: 2^31 - 1      */
  28. #define  Q       127773L    /*  M / A          */
  29. #define  R         2836L    /*  M % A          */
  30.  
  31.  
  32. static long _lseed = 1L;
  33.  
  34.  
  35. long seed( lseed )
  36.   long lseed;
  37.  
  38.   {
  39.   long previous_seed = _lseed;
  40.  
  41.   _lseed = lseed;
  42.  
  43.   return( previous_seed );
  44.   }
  45.  
  46.  
  47. long lrand()
  48.  
  49.   {
  50.   _lseed = A * (_lseed % Q) - R * (_lseed / Q);
  51.  
  52.   if ( _lseed < 0 )
  53.     _lseed += M;
  54.  
  55.   return( _lseed );
  56.   }
  57.